//
//  GYNetWorkTools.m
//  Visitor
//
//  Created by x on 2017/9/28.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYNetWorkTools.h"
#import "SDImageCache.h"

@implementation GYNetWorkTools

/**
 创建一个请求对象

 @return <#return value description#>
 */
+ (AFHTTPSessionManager *)Manager {
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://evisitors.baxcall.com:8443/visitorsystem/"]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    [manager setSecurityPolicy:[self customSecurityPolicy]];
    return manager;
}

+ (AFSecurityPolicy*)customSecurityPolicy
{
    // /先导入证书
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"cer"];//证书的路径
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    
    securityPolicy.allowInvalidCertificates = YES;
    
    securityPolicy.validatesDomainName = NO;
    securityPolicy.pinnedCertificates = [NSSet setWithObject:certData];
    
    
    return securityPolicy;
}


+ (void)loginWithUser:(NSString *)userName PassWord:(NSString *)pwd Success:(SuccessCallBack)successCallBack Filed:(ErrorCallBlck)errorCallBack {
    
    NSString *url = @"app/interviewee/login";
    NSDictionary *dic = @{@"loginName":userName,@"loginPassword":pwd};
    
    //3.发送一个post请求
    [[self Manager] POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
    
}

+ (void)loadVisitorData:(SuccessCallBack)successCallBack filed:(ErrorCallBlck)errorCallBack {
    NSString *url = @"app/visitor/getAccessRecord";
    NSDictionary *dic = @{@"requestKey":[[GYUserInfo loadUserInfo] requestKey]};
    [[self Manager] POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)handleVisitorRequest:(NSString *)requestKey operationCode:(NSString *)operationCode visitorId:(NSString *)visitorId cause:(NSString *)cause susscess:(SuccessCallBack)successCallBack filed:(ErrorCallBlck)errorCallBack {
    NSString *url = @"app/interviewee/disposeRequest";
    NSDictionary *dic = @{@"requestKey":requestKey,@"operationCode":operationCode,@"visitorId":visitorId,@"cause":cause};
    [[self Manager] POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)loginOut:(NSString *)requestKey susscess:(SuccessCallBack)successCallBack filed:(ErrorCallBlck)errorCallBack {

    NSString *url = @"app/interviewee/userLogout";
    NSDictionary *dic = @{@"requestKey":requestKey};
    [[self Manager] POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)uploadDeviceToken:(NSString *)deviceToken requestKey:(NSString *)requestKey susscess:(SuccessCallBack)successCallBack filed:(ErrorCallBlck)errorCallBack {

    NSString *url = @"app/interviewee/provideDeviceToken";
    NSDictionary *dic = @{@"deviceToken":deviceToken,@"requestKey":requestKey,@"type":@"1"};
    [[self Manager]POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)loadVisitorfaceimagebase:(NSString *)faceimagebase susscess:(ReturnImage)successCallBack filed:(ErrorCallBlck)errorCallBack {
    NSString *url = @"app/visitor/getImage";
    NSDictionary *dic = @{@"faceimagebase":faceimagebase,@"requestKey":[[GYUserInfo loadUserInfo] requestKey]};
    [[self Manager]POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        UIImage *image = [[UIImage alloc] initWithData:responseObject];
        [[SDImageCache sharedImageCache] storeImage:image forKey:faceimagebase];
        successCallBack(image);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}

+ (void)loadUserInfo:(SuccessCallBack)successCallBack filed:(ErrorCallBlck)errorCallBack {
    NSString *url = @"app/interviewee/getUserMessage";
    NSDictionary *dic = @{@"requestKey":[[GYUserInfo loadUserInfo] requestKey]};
    [[self Manager]POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        successCallBack(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorCallBack(error);
    }];
}
@end
